package com.txy.webmanagement.service;

import com.gitee.sunchenbin.mybatis.actable.dao.common.BaseMysqlCRUDMapper;
import com.gitee.sunchenbin.mybatis.actable.manager.common.BaseMysqlCRUDManager;
import com.txy.webmanagement.dto.SysMenuDTO;
import com.txy.webmanagement.mapper.SysMenuMapper;
import com.txy.webmanagement.model.SysMenu;
import com.txy.webmanagement.model.SysUser;
import com.txy.webmanagement.utils.RequestDataUtils;
import com.txy.webmanagement.utils.StringUtils;
import com.txy.webmanagement.utils.SysParrentUtils;
import com.txy.webmanagement.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private BaseMysqlCRUDManager baseMysqlCRUDManager;

    public List<SysMenu> findPageList(SysMenuDTO dto) {
        return sysMenuMapper.findPageList(dto.getMenuName());
    }

    public List<SysMenu> findFirstMenuList(String userTypeId) {
        return sysMenuMapper.findFirstList(userTypeId);
    }

    /**
     * 查询下级情况
     *
     * @param parentId
     * @return
     */
    public RequestDataUtils findSecendMenuList(String parentId) {
        RequestDataUtils req = new RequestDataUtils();
        if (StringUtils.checkStringNotNull(parentId)) {
            SysUser user = UserUtils.getLoginUser();
            String usertType = user.getTypeId();
            List<SysMenu> secontList = sysMenuMapper.findSecondByUserTypeIdAndParentId(usertType, parentId);
            req.setData(secontList);
            req.setCode(SysParrentUtils.successCode);
        } else {
            req.setCode(SysParrentUtils.errorCode);
            req.setMessage(SysParrentUtils.NoParent);
        }
        return req;
    }

    public RequestDataUtils save(SysMenuDTO dto) {
        RequestDataUtils req = new RequestDataUtils();
        SysMenu sm = new SysMenu();
        Date date = new Date();
        sm.setCreateTime(date);
        sm.setRows(1);
        sm.setMenuName(dto.getMenuName());
        sm.setEnable(1);
        sm.setHttpUrl(dto.getHttpUrl());
        sm.setIconStyle(dto.getIconStyle());
        sm.setParentId(dto.getParentId());
        baseMysqlCRUDManager.save(sm);
        req.setCode(SysParrentUtils.successCode);
        return req;
    }



}
